<template>
  <div class="center">
    <div class="search clearfix">
      <div class="block"></div>
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <div class="table">
      <dv-border-box-11 :color="['#11caff']" backgroundColor="transparent" title="角色信息">
      <el-table
        :data="tableData"
        height="71.5vh"
        class="customer-table"
        :header-cell-style="headClass"
      >
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column prop="name" label="角色类型"> </el-table-column>
        <el-table-column prop="roleName" label="角色名称" align="center">
        </el-table-column>
        <el-table-column label="创建时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.createTime.substring(0, 10) }}
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.updateTime.substring(0, 10) }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="change(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="primary" @click="accredit(scope.row)"
              >授权</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChange"
        @prev-click="prevclick"
        @next-click="nextclick"
        :current-page="currentPage"
        :page-size="pagesize"
        layout="total,prev, pager, next"
        :total="total"
      >
      </el-pagination>
      </dv-border-box-11>
    </div>

    <!-- 角色新增 -->
    <el-dialog
      class="actor-add"
      title="权限分配"
      :inline="true"
      :visible.sync="dialogFormVisible"
      :append-to-body="true"
    >
      <el-form
        class="clearfix"
        :model="addform"
        ref="addruleform"
        label-position="left"
        label-width="120px"
        :rules="addrules"
      >
        <el-form-item label="昵称">
          <el-input v-model="addform.name"></el-input> </el-form-item
        >
         <el-form-item label="租户账号" prop="tenantAccounts">
          <el-select v-model="addform.tenantAccounts">
            <el-option
              v-for="item in accountlist"
              :label="item.companyName"
              :key="item.index"
              :value="item.accounts"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色名(中文)" prop="roleName">
          <el-input v-model="addform.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input
            v-model="addform.roleDescription"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addactor(addform)">新增</el-button>
          <el-button @click="addout">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 角色编辑 -->
    <el-dialog
      class="actor-update"
      title="角色编辑"
      :inline="true"
      :visible.sync="dialogFormVisible2"
      :append-to-body="true"
    >
      <el-form
        class="clearfix"
        :model="updateform"
        ref="updateruleform"
        label-position="left"
        label-width="100px"
        :rules="updaterules"
      >
        <el-form-item label="name">
          <el-input v-model="updateform.name"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="updateform.roleDescription"></el-input>
        </el-form-item>
        <el-form-item label="角色名(中文)">
          <el-input v-model="updateform.roleName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="changeactor(updateform)">修改</el-button>
          <el-button @click="changeout">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 权限分配 -->
    <el-dialog
      class="power"
      title="权限分配"
      :inline="true"
      :visible.sync="dialogFormVisible3"
      :append-to-body="true"
    >
      <el-tree
        :data="powerlist"
        show-checkbox
        default-expand-all
        node-key="id"
        ref="tree"
        highlight-current
        :default-expanded-keys="expandarr"
        :default-checked-keys="checkarr"
        :props="defaultProps"
        @check-change="getChecked"
      >
      </el-tree>
      <el-button @click="accreditenter">修改</el-button>
      <el-button @click="accreditout">取 消</el-button>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      username: "",
      currentPage: 1,
      pagesize: 10,
      total: 0,
      tableData: [],
      powerlist: [],
      expandarr: [],
      checkarr: [],
      accountlist:[],
      dialogFormVisible: false,//角色新增弹窗
      dialogFormVisible2: false,//角色编辑弹窗
      dialogFormVisible3: false,//权限分配弹窗
      roleId: "",
      defaultProps: {
        children: "children",
        label: function (data, node) {
          return data.resName;
        },
      },
      addform: {
        name: "",
        roleDescription: "",
        roleName: "",
        tenantAccounts: "",
      },
      addrules: {
        tenantAccounts: [
          { required: true, message: "所属商家账号不能为空", trigger: "blur" },
        ],
        roleName: [
          { required: true, message: "角色名不能为空", trigger: "blur" },
        ],
      },
      updateform: {
        name: "",
        roleDescription: "",
        roleName: "",
        tenantAccounts: "",
      },
      updaterules: {},
      addruleform: {
        tenantAccounts: "",
        roleName: "",
      },
      updateruleform: {},
    };
  },
   watch:{
    currentPage(){
      this.getactor();
    }
  },
  methods: {
    headClass() {
      return "background-color: transparent;color:#fff;font-weight:bold;";
    },
    //获取角色列表
    getactor() {
      let params = {
        page: {
          pageNo: this.currentPage,
          pageSize: this.pagesize,
        },
      };
      this.$http
        .post("/role/list", params)
        .then((res) => {
          if (res.data.h.code == 0) {
            this.total = res.data.b.totalCount;
            this.tableData = res.data.b.result;
          }
        })
        .catch((err) => {});
    },
    //获取租户下拉列表
    getaccountlist(){
       this.$http
        .get("/tenant/tenantList", )
        .then((res) => {
          if (res.data.h.code == 0) {
            this.accountlist=res.data.b;
          }
        })
        .catch((err) => {});
    },
    //上一页
    prevclick() {
      this.currentPage--;
    },
    //下一页
    nextclick() {
      this.currentPage++;
    },
    //分页
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    //新增角色表单清空
    addformclear() {
      for (let i in this.addform) {
        this.addform[i] = "";
      }
    },
    //新增角色结束
    addout() {
      this.dialogFormVisible = false;
      this.$refs.addruleform.clearValidate();
      this.addformclear();
    },
    //新增角色开始
    add() {
      this.dialogFormVisible = true;
      this.getaccountlist();
    },
    addactor(form) {
      let params = {
        name: form.name,
        tenantAccounts:form.tenantAccounts,
        roleDescription: form.roleDescription,
        roleId: form.roleId,
        roleName: form.roleName,
      };
      this.$http
        .post("/role/add", params)
        .then((res) => {
          if (res.data.h.code == 0) {
            this.addout();
            this.getactor();
            this.$message({
              message: "新增成功",
              type: "success",
            });
          }
        })
        .catch((err) => {});
    },
    // 获取权限列表
    getpower() {
      this.$http
        .get("/router/list")
        .then((res) => {
          if (res.data.h.code == 0) {
            let oneArr = [],
              anotherArr = [];
            oneArr = res.data.b.filter((item) => !item.parentId); //一级路由
            anotherArr = res.data.b.filter((item) => item.parentId); //二级路由
            oneArr.map((obj) => {
              obj.children = [];
            });
            anotherArr.map((item) => {
              oneArr.map((obj) => {
                if (item.parentId == obj.id) {
                  obj.children.push(item);
                }
              });
            });
            this.powerlist = oneArr;
          }
        })
        .catch((err) => {});
    },
    //授权开始
    accredit(row) {
      this.checkarr=[];
      this.roleId = row.roleId;
      this.$http
        .get("/router/router/" + this.roleId)
        .then((res) => {
          if (res.data.h.code == 0) {
            this.dialogFormVisible3 = true;
            let newarr = [];
            let home=res.data.b[0].id
            res.data.b.map((item) => {
              if(item.parentId!="0"&&item.resKey!="home"){
                newarr.push(item.id);
              }
            });
            newarr.push(home)
            this.checkarr=newarr
          }
        })
        .catch((err) => {});
    },
    accreditenter() {
      let list = this.getChecked();
      let newarr = [];
      list.map((item) => {
        newarr.push(item.id);
      });
      let params = {
        roleId: this.roleId,
        routers: newarr,
      };
      this.$http
        .post("/role/author", params)
        .then((res) => {
          if (res.data.h.code == 0) {
            this.$message({
              message: "授权成功",
              type: "success",
            });
            this.accreditout();
          }
        })
        .catch((err) => {});
    },
    accreditout() {
      this.dialogFormVisible3 = false;
      this.$refs.tree.setCheckedKeys([]);
    },
    getChecked() {
      let res = this.$refs.tree.getCheckedNodes();
      let half = this.$refs.tree.getHalfCheckedNodes();
      let selectlist = res.concat(half);
      return selectlist;
    },
    //编辑角色开始
    change(row) {
      this.dialogFormVisible2 = true;
      this.updateform = Object.assign(this.updateform, row);
    },
    //编辑角色表单清空
    updateformclear() {
      for (let i in this.updateform) {
        this.updateform[i] = "";
      }
    },
    //编辑结束
    changeout() {
      this.$refs.updateruleform.clearValidate();
      this.dialogFormVisible2 = false;
      this.updateformclear();
    },
    changeactor(form) {
      let params = {
        name: form.name,
        roleDescription: form.roleDescription,
        roleId: form.roleId,
        roleName: form.roleName,
        tenantAccounts: form.tenantAccounts,
      };
      this.$refs.updateruleform.validate((valid) => {
        if (valid) {
          this.$http
            .post("/role/update", params)
            .then((res) => {
              if (res.data.h.code == 0) {
                this.changeout();
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                this.getactor();
              }
            })
            .catch((err) => {});
        } else {
          this.$message({
            message: "表单填写有误，请重新填写",
            type: "warning",
          });
          return false;
        }
      });
    },
    //删除角色
    handleDelete(row) {
      this.$confirm("此操作将删除该角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {
            roleId: row.roleId,
          };
          this.$http.post("/role/del", params).then((res) => {
            if (res.data.h.code == 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              //重新请求员工列表
              this.getactor();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.getpower();
  },
  mounted() {
    this.getactor();
  },
};
</script>
<style scoped>
.center {
  position: relative;
}
.search {
  padding-left: 10px;
  box-sizing: border-box;
  display: inline-block;
}
.block {
  display: inline-block;
  margin-right: 10px;
}
.table {
  width: 100%;
  height:83vh;
}
.table.cell > span {
  cursor: pointer;
  color: #007bff;
}
.table .el-table {
  width: 100%;
  height: 93%;
}
.el-pagination {
  text-align: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
.actor-update .el-form {
  padding: 15px;
  box-sizing: border-box;
}
.actor-add .el-form .el-form-item{
  display: inline-block;
  width:45%;
}
.actor-add .el-form .el-form-item .el-select{
  display: block;
}
.actor-add .el-form .el-form-item:nth-of-type(2n+1){
  margin-right:2%;
}
.el-tree {
  background: transparent;
  color: #fff;
  border-bottom: 1px solid #13519c;
  margin-bottom: 20px;
}
.el-tree /deep/.el-tree-node:focus > .el-tree-node__content {
  background-color: #66b1ff87 !important;
}
.el-tree /deep/ .el-tree-node__content:hover {
  background-color: #66b1ff87;
}
.el-tree
  /deep/.el-tree--highlight-current
  .el-tree-node.is-current
  > .el-tree-node__content {
  background-color: #66b1ff87;
}

.el-tree-node:focus > .el-tree-node__content {
  background-color: #66b1ff87 !important;
}
.el-tree-node__content:hover {
  background-color: #66b1ff87;
}
.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
  background-color: #66b1ff87;
}
.el-dialog .el-button {
  background: transparent;
}
.el-button--default {
  background: transparent;
  color: #00ffff;
  border-color: rgba(19, 81, 156, 1) !important;
}
</style>
<style>
.customer-table th {
  border: none;
}
.customer-table td,
.customer-table th.is-leaf {
  border: none;
}
</style>